/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.tom.social.common.persist;

import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Tom
 */
public class CsvDumper {
	//TODO change to filedumper
	private static final Logger logger = Logger.getLogger(CsvDumper.class);

	public static void dump(JTable table, String fileName) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(fileName);
			for(int row = 0; row < table.getRowCount(); row++) {
				for(int col = 0; col < table.getColumnCount() - 1; col++) {
					fw.append(table.getValueAt(row, col).toString() + ",");
				}
				fw.write(table.getValueAt(row, table.getColumnCount() - 1).toString() + "\n");
			}
		} catch(IOException ex) {
			logger.log(Level.ERROR, null, ex);
		} finally {
			try {
				fw.close();
			} catch(IOException ex) {
				logger.log(Level.ERROR, null, ex);
			}
		}
	}
}
